package com.king.headline.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.beans.DetailInfo;
import com.king.headline.util.Configuration;
import com.king.headline.util.DialogUtils;
import com.king.headline.util.UIUtils;
import com.king.headline.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * 新增预约
 * Created by king on 2015/12/3.
 */
public class CommentAdapter extends BaseAdapter {
    private String sourceid;
    private  ImageLoader imageLoader;
    private int personId;
    private  Context context;
    private  ArrayList<DetailInfo.Commentist> list;

    public CommentAdapter(Context context, String sourceid,ArrayList<DetailInfo.Commentist> list) {
        this.context = context;
        this.sourceid = sourceid;
        this.list = list;
        imageLoader = UIUtils.getImageLoader();
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_comment, null);
            holder.tv_info = (TextView) convertView.findViewById(R.id.tv_info);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.iv_touxiang = (CircleImageView) convertView.findViewById(R.id.iv_touxiang);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DetailInfo.Commentist commentInfo = list.get(position);
        if (!commentInfo.submit.chatHeadurl.equals( holder.iv_touxiang.getTag())) {
            // 如果不相同，就加载。现在在这里来改变闪烁的情况
            imageLoader.displayImage(Configuration.API_HOST + commentInfo.submit.chatHeadurl, holder.iv_touxiang, UIUtils.getSimpleOption());
            holder.iv_touxiang.setTag(commentInfo.submit.chatHeadurl);
        }
        holder.tv_name.setText(commentInfo.submit.username);
        holder.tv_time.setText(commentInfo.addTime);
        holder.tv_info.setText(commentInfo.comment);

        if (commentInfo.replyTo!=null){
            holder.tv_info.setText("回复 " + commentInfo.replyTo.username + "：" + commentInfo.comment);
            holder.tv_info.setText(Html.fromHtml("回复<font color=\"#3475cf\">"+ commentInfo.replyTo.username +"</font>："+ commentInfo.comment));
        }else{
            holder.tv_info.setText(commentInfo.comment);
        }
        holder.tv_comment.setTag(R.id.tag_first,"回复"+commentInfo.submit.username );
        holder.tv_comment.setTag(R.id.tag_second,commentInfo.submit.encyptdId );
        holder.tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String hintText = (String) v.getTag(R.id.tag_first);
                String encyptdId = (String) v.getTag(R.id.tag_second);
                DialogUtils.setTag(sourceid +"::::" +encyptdId);
                DialogUtils.showModifyCommentDialog(context, hintText);
            }
        });
        return convertView;
    }


    public void setData( ArrayList<DetailInfo.Commentist> list){
        this.list = list;
        this.notifyDataSetChanged();

    }

    private class ViewHolder {
        public TextView tv_info;
        public TextView tv_name;
        public TextView tv_time;
        public CircleImageView iv_touxiang;
        public TextView tv_comment;
    }


}
